// pages/details/details.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
var WxParse = require('../../common/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:'',//帖子id
    detailObj:{},
    postCon:'',
    reCon:'',
    postfocus:false,//帖子回复
    refocus:false,//子回复
    comment_id:'',
    is_collect:"",
    page:{
      p:0,
      psize:15
    },
    hei:0,
    shars:false,
    playing:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {
        // 分享成功
        console.log('shareMenu share success')
        console.log('分享' + res)
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    })
    if (options.shars){
      this.setData({
        shars:true
      })
    }
    this.setData({
      id:Number(options.id)
    })
    wx.getSystemInfo({
      success: res => {
        let windowHeight = (res.windowHeight * (750 / res.windowWidth));
        this.setData({
          hei: windowHeight -80
        })
      }
    })
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onReady: function () {  //创建视频上下文对象
    this.videoContext = wx.createVideoContext('myVideo')
  },
  plays(){
    let play=this.data.playing;
    if (play) {
      play = false
      this.videoContext.stop()
    } else {
      play = true
      this.videoContext.play()
    }
    this.setData({
      playing:play
    })
  },
  gzimg(e){
    let list = e.currentTarget.dataset.list,
      img = e.currentTarget.dataset.img;
    wx.previewImage({
      urls: list,
      current:img
    })
  },
  init:function(refresh = false){
    let that=this;
    wx.showLoading({
      success:()=>{
      wx.request({
        url: base_url + '/Post/detail',
        data: {
          'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
          'id': this.data.id,
          'p':this.data.page.p,
          'psize':this.data.page.psize
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.success == '1' && res.data.data) {
            this.setData({
              detailObj: res.data.data
            })
            WxParse.wxParse('article', 'html', res.data.data.content, that);
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          wx.hideLoading()
          
        }
      })
      }
    })
  },
  remove(){
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Post/del',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': this.data.id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            if (res.data.success == '1') {
              wx.showToast({
                title: '删除成功!',
                icon: 'success',
                duration: 2000
              })
              setTimeout(e=>{
                wx.reLaunch({
                  url: '../index/index'
                })
              },2200)
             
            } else {
              wx.showToast({
                title: '服务器出错啦',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  selected(){
    wx.request({
      url: base_url + '/Post/collectOrCancel',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'post_id': this.data.id,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          let txt=new String();
          if (this.data.detailObj.is_collect==0){
            txt ="收藏成功";
            this.setData({
              'detailObj.is_collect':1
            })
          }else{
            txt = "取消收藏成功";
            this.setData({
              'detailObj.is_collect': 0
            })
          }
          wx.showToast({
            title:txt,
            icon: 'success',
            duration: 2000
          })
          this.setData({
            selected:true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          
        }
      }
    })
  },
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      let id = this.data.id;
      // 来自页面内转发按钮
      return {
        title: '大家一起来看看哈，我家的老狗',
        path: '/pages/details/details?id=' + id+"&shars=1",
        success: function (res) {
          console.log(res)
          // 转发成功
          // var shareTickets = res.shareTickets;
          // if (shareTickets.length == 0) {
          //   return false;
          // }
          wx.getShareInfo({
            shareTicket: shareTickets[0],
            success: function (res) {
              var encryptedData = res.encryptedData;
              var iv = res.iv;
            }
          })
        },
        fail: function (res) {
          // 转发失败
          wx.showToast({
            icon: 'none',
            title: '转发失败',
          })
        }

      }
    }
  },
  want(){
    wx.navigateTo({
      url: '../dogCard/dogCard?id=' + this.data.detailObj.user_id
    })
  },
  bindButtonTap(e){//唤起键盘
   
    let type = e.currentTarget.dataset.type
    if(type == 0){
      this.setData({
        postfocus:true
      })
    }else if(type == 1){//子回复
      this.setData({
        refocus: true,
        comment_id: e.currentTarget.dataset.commentid
      })
    }
  },
  commit(e){//回复帖子
    this.setData({
      postCon: e.detail.value
    })
  
      if (this.data.postCon == '') {
        this.setData({
          postfocus: false
        })
        return
      }
    wx.showModal({
      title: '提示',
      content: '确认发起该评论吗？',
      success: (res)=> {
        if (res.confirm) {
          wx.showLoading({
            success: () => {
              wx.showLoading({
                success: () => {
                  wx.request({
                    url: base_url + '/Post/comment',
                    method: 'POST',
                    data: {
                      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
                      'post_id': this.data.id,
                      'content': e.detail.value
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: res => {
                      wx.hideLoading()
                      this.setData({
                        postCon: '',
                        postfocus: false
                      })
                      if (res.data.success == '1') {
                        
                        this.init()
                      } else {
                        wx.showToast({
                          title: '服务器出错啦',
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    }
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
    
  },
  recommit(e){//回复子评论
    this.setData({
      reCon: e.detail.value
    })
    if (this.data.reCon == '') {
      this.setData({
        refocus:false
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '这是一个模态弹窗',
      success: (res)=> {
        if (res.confirm) {
          wx.showLoading({
            success: () => {
              wx.request({
                url: base_url + '/Post/comment',
                method: 'POST',
                data: {
                  'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
                  'comment_id': this.data.comment_id,
                  'content': e.detail.value
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                  this.setData({
                    reCon: '',
                    refocus: false
                  })
                  wx.hideLoading()
                  if (res.data.success == '1') {
                    this.init()
                  } else {
                    wx.showToast({
                      title: '服务器出错啦',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                }
              })
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
    
  },
  handleZan: function () {//评论点赞
    util.zan({
      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'post_id': this.data.detailObj.post_id
    }, (res) => {
      let is_zan = 'detailObj.is_zan';
      let zan_num = 'detailObj.zan_num';
      let num = Number(this.data.detailObj.zan_num);
      // this.setData({
      //   [is_zan]: this.data.detailObj.is_zan == 0 ? 1 : 0,
      //   [zan_num]: this.data.detailObj.is_zan == 0 ? num + 1 : num - 1
      // })
      this.init();
    })
  },
  focus:function(e){//关注用户
    let id = this.data.detailObj.user_id;
    let is_follow = this.data.detailObj.is_follow;
    if(is_follow==0){
      this.tofoucs(id,is_follow);
    }else{
      wx.showModal({
        title: '提示',
        content: '确认取消关注吗？',
        success: res=>{
          if (res.confirm) {
            
            this.tofoucs(id, is_follow);
          } 
        }
      })
    }
    
   

  },
  tofoucs(id,types){
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Users/followOrCancel',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'follow_uid': id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            // this.setData({
            //   'detailObj.is_follow': 1,
            // })
            wx.hideLoading()
            if (res.data.success == '1') {
              if(types==0){
                wx.showToast({
                  title: '关注成功',
                  icon: 'success',
                  duration: 2000
                })
                this.setData({
                  'detailObj.is_follow':1
                })
              }else{
                this.setData({
                  'detailObj.is_follow': 0
                })
              }
              //this.init()
            } else {
              wx.showToast({
                title: '服务器出错啦',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  handlerecomZan:function(e){//子评论点赞
    let id = e.currentTarget.dataset.id;
    util.zan({
      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'comment_id': this.data.detailObj.comment[id].comment_id
    },res=>{
      let is_zan = 'detailObj.comment['+id+'].comment_is_zan';
      let zan_num = 'detailObj.comment[' + id +'].comment_zan_num';
      let num = Number(this.data.detailObj.comment[id].comment_zan_num);
      // this.setData({
      //   [is_zan]: this.data.detailObj.comment[id].comment_is_zan == 0 ? 1 : 0,
      //   [zan_num]: this.data.detailObj.comment[id].comment_is_zan == 0 ? num + 1 : num - 1
      // })
      this.init();
    })
  }
})